package org.hook.showcase.pattern.iterator.impl;

import org.hook.showcase.pattern.iterator.Iterator;

public class DinerMenuIterator implements Iterator {
	private MenuItem[] menuItmes;
	private int position = 0;

	public DinerMenuIterator(MenuItem[] menuItmes) {
		this.menuItmes = menuItmes;
	}

	@Override
	public boolean hasNext() {
		if (this.menuItmes == null || this.position > this.menuItmes.length || this.menuItmes[this.position] == null)
			return false;
		else
			return true;
	}

	@Override
	public Object next() {
		MenuItem menuItem = this.menuItmes[this.position];
		this.position = this.position + 1;
		return menuItem;
	}

	@Override
	public void remove() {
		if (this.menuItmes == null || this.position < 0) {
			throw new IllegalStateException("not support.");
		}
		int index = this.position - 1;
		if (this.menuItmes[index] != null) {
			int length = this.menuItmes.length - 1;
			for (int i = index; i < length; i++) {
				this.menuItmes[i] = this.menuItmes[i + 1];
			}
			this.menuItmes[length] = null;
		}
	}
}
